package com.example.rabbitmq.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    // 交换机名称
    public static final String BOOT_EXCHANGE = "boot_exchange";
    // 队列名称
    public static final String BOOT_QUEUE = "boot_queue";
    // 路由键
    public static final String BOOT_ROUTING_KEY = "boot.info";

    // 声明交换机
    @Bean("bootExchange")
    public Exchange bootExchange() {
        return ExchangeBuilder.directExchange(BOOT_EXCHANGE)
                .durable(true)  // 持久化
                .build();
    }

    // 声明队列
    @Bean("bootQueue")
    public Queue bootQueue() {
        return QueueBuilder.durable(BOOT_QUEUE)
                .build();
    }

    // 绑定交换机和队列
    @Bean
    public Binding bindQueueExchange(
            @Qualifier("bootQueue") Queue queue,
            @Qualifier("bootExchange") Exchange exchange) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with(BOOT_ROUTING_KEY)
                .noargs();
    }
}
